package com.mk.cloud.common.controller;

import act.apidoc.Description;
import act.controller.Controller;
import act.validation.NotBlank;
import com.mk.cloud.common.domain.CpSsorolePlatform;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import com.mk.cloud.pt.dto.CpSsorolePlatformDTO;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Description("平台角色维护")
@Controller("/cpSsorolePlatform")
public class CpSsorolePlatformController extends CookieService{

    @Inject
    private CpSsorolePlatform.Mapper cpSsorolePlatformMapper;

    @GetAction("list.json")
    @With(ICLogin.class)
    @Description("平台-角色-获取全部有效角色列表（已实现）")
    public List<CpSsorolePlatformDTO> listSsorolePlatform(){
        List<CpSsorolePlatformDTO> list = cpSsorolePlatformMapper.selectAll();
        Map<String,CpSsorolePlatformDTO> map = new HashMap<>();
        List<CpSsorolePlatformDTO> result = new ArrayList<>();
        list.forEach( dto ->{
            findSon(dto,list);
            if(S.isBlank(dto.getRolepid())){
                result.add(dto);
            }
        });
        return result;
    }

    /**
     * 递归的将所有子类都赋予父类的属性
     * @param dto
     * @param list
     */
    private void findSon(CpSsorolePlatformDTO dto,List<CpSsorolePlatformDTO> list){
        List<CpSsorolePlatformDTO> sublist = list.stream().filter(
                d->S.eq(d.getRolepid(),(dto.getRoleid()))
        ).collect(Collectors.toList());
        if(!sublist.isEmpty()){
            sublist.forEach(subdto->{
                findSon(subdto,list);
            });
            dto.setListDTO(sublist);
        }
    }
    @GetAction("{roleid}/roleplat.json")
    @Description("查询平台角色")
    @With(ICLogin.class)
    public CpSsorolePlatform roleplat(@Description("角色id")String roleid){
        return cpSsorolePlatformMapper.selectByRoleid(roleid);
    }

    @PostAction("add.json")
    @With(ICLogin.class)
    @Description("平台-角色-新增平台角色,返回新增的角色主键（已实现）")
    public String addSsorolePlatform(
            @Description("平台角色对象")
            CpSsorolePlatform cpSsorolePlatform
    ){
        String uuid = S2.UUID();
        cpSsorolePlatform.setRoleid(uuid);
        UserException.throwIf(S.isBlank(cpSsorolePlatform.getRolecode()), ErrorCode.E200000);
        cpSsorolePlatformMapper.insertTemplate(cpSsorolePlatform);
        return uuid;
    }

    @PutAction("update.json")
    @With(ICLogin.class)
    @Description("平台-角色-修改平台角色,返回角色主键（已实现）")
    public String updateSsorolePlatform(
            @Description("平台角色对象")
            CpSsorolePlatform cpSsorolePlatform
    ){
        cpSsorolePlatformMapper.updateTemplateById(cpSsorolePlatform);
        return cpSsorolePlatform.getRoleid();
    }

    @DeleteAction("{roleid}/del.json")
    @With(ICLogin.class)
    @Description("平台-角色-删除平台角色(已实现)")
    public int deleteSsorolePlatform(
            @Description("平台角色主键")
            String roleid
    ){
        return cpSsorolePlatformMapper.deleteRolePlatform(roleid);
    }


    @PostAction("saveSsorolePlatformResource.json")
    @With(ICLogin.class)
    @Description("平台-角色-分配资源给平台角色(已实现)")
    public int saveSsorolePlatformResource(
            @Description("角色id")
            @NotBlank String roleid,
            @Description("平台资源id列表") List<String> resourceidList
    ){
        return cpSsorolePlatformMapper.saveRoleResourcePlatform(roleid,resourceidList);
    }

    @PostAction("saveRoleStructurePlatform.json")
    @With(ICLogin.class)
    @Description("平台-角色-保存平台角色结构(已实现)")
    public int saveRoleStructurePlatform(
            @Description("平台角色资源对象")
            List<CpSsorolePlatform> cpSsorolePlatforms
    ){
        return cpSsorolePlatformMapper.saveRoleStructurePlatform(cpSsorolePlatforms);
    }


}
